# Create your views here.
from django.shortcuts import render_to_response
from django.shortcuts import redirect
import time
import random
import urllib

def weaktoken1(request):
    content = {}

    if request.method == "POST":
        if str(request.POST['user']) == 'Lassie' and str(request.POST['pass']) == 'woofwoof':
            content['message'] = "Login Successful"
            response = render_to_response('Lab6/weaktoken1.html', content)
            sessionid = "Lassie:woofwoof:niceworkfindingthis".encode("hex")
            response.set_cookie("sessionid", sessionid)

            return response
        else:
            content['message'] = "Login failed.. Seriously?"
            return render_to_response('Lab6/weaktoken1.html', content)
    else:
        return render_to_response('Lab6/weaktoken1.html')


def weaktoken2(request):
    content = {}

    if request.method == "POST":
        if str(request.POST['user']) == 'Lassie' and str(request.POST['pass']) == 'woofwoof':
            content['message'] = "Login Successful"
            sessionid = "Lassie:woofwoof:niceworkfindingthis".encode("base64").replace('\n', '')

            response = render_to_response('Lab6/weaktoken2.html', content)

            response.set_cookie("sessionid", urllib.quote(sessionid))       # The urllib.quote() is used to strip the " from the cookie.

            return response
        else:
            content['message'] = "Login failed.. Seriously?"
            return render_to_response('Lab6/weaktoken2.html', content)
    else:
        return render_to_response('Lab6/weaktoken2.html')

def timetoken(request):
    sessionid = str(int(random.random() * 1000000)) + "-" + str(int(time.time()*1000000))

    if request.COOKIES.has_key("lab6-timetoken-sessionid"):
        sessionid = str(int(str(request.COOKIES['lab6-timetoken-sessionid']).split('-')[0]) + 1) + "-" + str(int(time.time()*1000000))

    response = render_to_response('Lab6/timetoken.html')
    response.set_cookie("lab6-timetoken-sessionid", urllib.quote_plus(sessionid))       # The urllib.quote() is used to strip the " from the cookie.

    return response

def weakrandom(request):

    # First create random number for the session
    sessionid = ""

    if request.COOKIES.has_key('Lab6-weakrandom-sessionid'):
        # We're going to create a new sessionid by multiplying it by a value, subtracting it by a value, and using the first 15 digits
        sessionid = int(request.COOKIES['Lab6-weakrandom-sessionid'])       # grab the current sessionid
        sessionid = sessionid * 42
        sessionid = sessionid - 10
        sessionid = str(sessionid)[:15]

    else:
        # If this is the first time the user sees the page, generate a random 15 digit sessionid
        sessionid = str(int(random.random()*100000000000000))

    response = render_to_response("Lab6/weakrandom.html")
    response.set_cookie("lab6-weakrandom-sessionid", urllib.quote_plus(sessionid))      # The urllib.quote() is used to strip the " from the cookie.

    return response